// pages/product/product.js
const app = getApp()
var postLogin = require("../../utils/wh.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    limit_num: 6,//每頁數量
    idxpage: 1,//第幾頁
    list: [],//數據列表
    url: "",
    categoryList: [],//产品类别
    currentTab: 0,
    idx:"0",//当前选择
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    wx.showLoading({
      title: '加载中•••',
      // mask: true
    })
    this.loop();
  },
  loop: function () {
    var that = this;
    var code = app.globalData.code;
    var url = app.globalData.URL;
    if (!code || !url) {
      var that = this
      setTimeout(function () { that.loop(); }, 100)
    } else {
      that.setData({
        code: code,
        url: url,
      });
      var categoryUrl = '/api/company.index/getProCate';//产品类别
      var postUrl = '/api/company.index/product',
        postData = {
          page: that.data.idxpage,
          limit_num: that.data.limit_num,
          cate_id: that.data.cate_id,
        };
      postLogin.Post(categoryUrl, '', that.category);
      postLogin.Post(postUrl, postData, that.product);

    }
  },
  category: function (res) {
    this.setData({
      categoryList:res
    })
  },
  product: function (res) {
    let that = this;
    var list = res.list;
    wx.hideLoading();
    that.setData({
      list: list,
    })
  }
  ,
  /** 
 * 点击tab切换 
 */
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  choose: function (event) {
    var that = this ;
    var idx = event.target.dataset.hi;
    this.setData({
      idx: idx,
    });
    var postUrl = '/api/company.index/product',
      postData = {
        page: that.data.idxpage,
        limit_num: that.data.limit_num,
        cate_id: that.data.idx,
      };
    postLogin.Post(postUrl, postData, that.product);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //下拉
    let that = this;
    that.loop();
    wx.stopPullDownRefresh();
  },  

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let i = this.data.idxpage + 1;
    this.setData({
      idxpage: i
    })
    this.loop();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})